#include "Boss.h"
#include "../SGD Wrappers/SGD_GraphicsManager.h"
#include "Game.h"
#include "GameplayState.h"
#include "Player.h"
#include <fstream>
#include <ShlObj.h>
#include <Shlwapi.h>
#pragma comment( lib, "shlwapi.lib")

void Boss::Render()
{
	SGD::GraphicsManager* gfx = SGD::GraphicsManager::GetInstance();

	float fillBar = GetPosition().x + GetSize().width * m_iHealth / m_iMaxHealth;
	SGD::Rectangle hpBar = SGD::Rectangle(GetPosition().x - Game::GetInstance()->GetCameraPos().x, GetPosition().y - 8, GetPosition().x - Game::GetInstance()->GetCameraPos().x + GetSize().width, GetPosition().y - Game::GetInstance()->GetCameraPos().y);
	SGD::Rectangle currHp = SGD::Rectangle(GetPosition().x - Game::GetInstance()->GetCameraPos().x, GetPosition().y - 8, fillBar - Game::GetInstance()->GetCameraPos().x, GetPosition().y - Game::GetInstance()->GetCameraPos().y);
	
	//draw the outline for the max healthbar
	gfx->DrawRectangle(hpBar, { 0, 0, 0, 0 }, { 255, 0, 0, 0 });
	
	//check which color the helath bar of the boss should be rendered as based on the amount of hp missing
	if ((float)m_iHealth/m_iMaxHealth >= 2.0f/3.0f)
		gfx->DrawRectangle(currHp, { 255, 255, 0 });//display yellow if its over 2/3 of the boss's maxhp
	else if ((float)m_iHealth / m_iMaxHealth >= 1.0f / 3.0f)
		gfx->DrawRectangle(currHp, { 255, 140, 0 });//display orange if its below 2/3 of the boss's maxhp and above 1/3 of the boss's max hp
	else
		gfx->DrawRectangle(currHp, { 255, 0, 0 });//display red if its below 1/3 the boss's max hp

	//Enemy::Render();
}

void Boss::SetPlayerNextLevel()
{
	if (GameplayState::GetInstance()->GetPlayer()->GetNextLevel() < GetLevel() + 1)
		GameplayState::GetInstance()->GetPlayer()->SetNextLevel(GetLevel() + 1);
}

void Boss::SaveHighScore(void)
{
	wchar_t* path = nullptr;
	SHGetKnownFolderPath(FOLDERID_LocalAppData, NULL, NULL, &path);
	wstring filename = path;
	filename.append(L"\\MegaLands\\");
	char newStr[300];
	wcstombs_s(nullptr, newStr, filename.c_str(), 300);

	//Create directory if it does not exist
	if (!PathFileExists(filename.c_str()))
		CreateDirectory(filename.c_str(), NULL);
	
	int score = GameplayState::GetInstance()->GetPlayer()->GetScore();
	
	for (int current = 0; current < 10; current++)
	{
		if (score > Game::GetInstance()->GetScore(level - 1, current))
		{
			for (int old = 9; old > current; old--)
			{
				Game::GetInstance()->SetName(level - 1, old, Game::GetInstance()->GetName(level - 1, old - 1));
				Game::GetInstance()->SetScore(level - 1, old, Game::GetInstance()->GetScore(level - 1, old - 1));
				Game::GetInstance()->SetCombo(level - 1, old, Game::GetInstance()->GetCombo(level - 1, old - 1));
			}
			Game::GetInstance()->SetName(level - 1 , current, GameplayState::GetInstance()->GetPlayer()->GetName());
			Game::GetInstance()->SetScore(level - 1, current, score);
			Game::GetInstance()->SetCombo(level - 1, current, GameplayState::GetInstance()->GetPlayer()->GetMaxCombo());
			break;
		}
	}
	
	switch (level)
	{
	case 1:
		filename.append(L"forest.txt");
		break;
	case 2:
		filename.append(L"cave.txt");
		break;
	case 3:
		filename.append(L"castle.txt");
		break;
	case 4:
		filename.append(L"volcano.txt");
		break;
	default:
		return;
		break;
	}

	ofstream fs(filename, ios_base::trunc);
	if (fs.is_open())
	{
		fs << Game::GetInstance()->GetLevel(level - 1) << endl;
		for (int i = 0; i < 10; i++)
		{
			fs << Game::GetInstance()->GetName(level - 1, i) << '\t' << Game::GetInstance()->GetCombo(level - 1, i) << '\t' << Game::GetInstance()->GetScore(level - 1, i) << endl;
		}
	}
}